package com.sl.core.engine.log;

import cn.hutool.core.util.ObjectUtil;
import com.sl.core.engine.event.handler.ProcessLogHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.hop.core.logging.HopLoggingEvent;
import org.apache.hop.core.logging.IHopLoggingEventListener;

@Slf4j
public class ProcessLoggingEventListener implements IHopLoggingEventListener {


    public static ProcessLogHandler INSTANCE = null;

    @Override
    public void eventAdded(HopLoggingEvent event) {

        ProcessLoggingEvent processLoggingEvent = null;
        if (!(event instanceof ProcessLoggingEvent)) {
            processLoggingEvent = new ProcessLoggingEvent(event);
        } else {
            processLoggingEvent = (ProcessLoggingEvent) event;
        }

        //输出到文件 怎么都输出 流程详细日志
        String format = ProcessLoggingBuffer.getInstance().getLayout().format(processLoggingEvent);
        log.info(format);

        String tid = processLoggingEvent.getTid();
        if (ObjectUtil.isNotNull(INSTANCE)) {
            //这里要给前端可视化的
            ProcessLogLayout layout = (ProcessLogLayout) ProcessLoggingBuffer.getInstance().getLayout();
            try{
                format = layout.formatJson(processLoggingEvent);
                INSTANCE.log(format, tid);
            }catch (Exception e){
                log.info("转换成json失败",e);
            }
        }

    }
}
